package faircat.android.exphone;

import java.lang.Class;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.content.Context;

import android.util.Log;

public class ScoSocketProxy {

	Object obj;
	private PowerManager mPowerManager;
	private Context mContext;
	
	public ScoSocketProxy(Context context, Handler handler, int acceptedCode, int connectedCode,
			int closedCode){
		
		mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
		mContext = context;
		
		try{
			Class cls = Class.forName("android.bluetooth.ScoSocket");
			Class[] par = new Class[]{PowerManager.class, Handler.class, int.class,int.class,int.class};
			Constructor con = cls.getConstructor(par);
			Object[] objs = new Object[]{mPowerManager, handler, acceptedCode, connectedCode, closedCode};
			obj = con.newInstance(objs);			
		}
		catch(Exception e){
			Log.e("HiddenScoSocket", "constructor failed", e);
		}
	}
	
	public boolean connect(String address, String name){
		try{
			Method acceptMethod = obj.getClass().getMethod("connect");
			return ((Boolean)acceptMethod.invoke(obj, address, name)).booleanValue();
		}
		catch(Exception e){
			
		}
		return false;
	}
	
	public boolean accept(){
		try{
			Method acceptMethod = obj.getClass().getMethod("accept");
			return ((Boolean)acceptMethod.invoke(obj)).booleanValue();
		}
		catch(Exception e){
			
		}
		return false;
	}
}
